// components/million-buy/index.js
let url_js = require("../../utils/url.js");
let common_js = require("../../utils/common.js");

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object,
      value: {}
    }
  },

  observers: {
    'info': function (item) {
      //如果需要开启定时器
      if (item) {
        //进度条
        this.initProgress(item);
        // 开启倒计时
        if (item.productInfo && item.productInfo.groupInfo) {
          this.initTimerObj(item);
        }
        //显示
        this.setData({
          hidden: false
        })
      } else {
        this.clearTimer();
        //显示
        this.setData({
          hidden: true
        })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    hidden: true,
    timerObj: {
      timerId: null,
      isEnd: false,
      endTime: '',
      item: null
    },
    percent: 0,
    bgImgUr: url_js.imgUrl + "wrt_bg.png",
    logoImgUr: url_js.imgUrl + "wrt_logo.png",
    arrowImgUr: url_js.imgUrl + "wrt_price_qx.png",
  },

  // 页面生命周期
  lifetimes: {
    // 页面销毁
    detached() {
      this.clearTimer();
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 初始化进度条
    initProgress(item) {
      if (!item.process) return;
      
      let proress = (parseFloat(item.process) * 100).toFixed(2);
      this.setData({
        percent: proress
      });
    },
    // 初始化
    initTimerObj(item) {
      let expire = 0;
      if (item.productInfo.groupInfo.status == 1) {
        //预热
        expire = item.productInfo.groupInfo.wait;
      } else if (item.productInfo.groupInfo.status == 2) {
        // 进行中
        expire = item.productInfo.groupInfo.expire;
      }
      if (expire <= 0) return;
      let tmpTimerObj = this.data.timerObj;
      if (expire > 0) {
        tmpTimerObj.endTime = new Date().getTime() + expire * 1000;
      }
      //set dasta
      this.setData({
        timerObj: tmpTimerObj
      });
      // 开启计时器
      this.countDownTimer();
    },
    // 计时器**********
    countDownTimer() { //倒计时函数
      // 获取当前时间，同时得到活动结束时间数组
      let newTime = new Date().getTime();
      let tmpTimerObj = this.data.timerObj;
      let endTime = tmpTimerObj.endTime;
      let time = (endTime - newTime) / 1000;
      // 如果活动未结束，对时间进行处理
      let hmsObj = common_js.getHrMinSecBySecond(time);
      tmpTimerObj.item = hmsObj.item;
      tmpTimerObj.isEnd = hmsObj.isEnd;
      // 先清除计时器
      this.clearTimer();
      //1s调用一次
      let that = this;
      let timerId = setTimeout(function () {
        that.countDownTimer();
      }, 1000);
      tmpTimerObj.timerId = timerId;
      // 渲染，然后每隔一秒执行一次倒计时函数
      this.setData({
        timerObj: tmpTimerObj
      })
      // 清除
      if (tmpTimerObj.isEnd) {
        this.clearTimer();
        this.checkCountDownDone();
      }
    },
    // 检查倒计时时候完成
    checkCountDownDone() {
      if (this.data.info.productInfo.groupInfo.status == 1) {
        //预热
        let tmpItem = this.data.info;
        tmpItem.productInfo.groupInfo.status = 2;
        // 重置
        let timeObj = this.data.timerObj;
        timeObj.isEnd = false;
        this.setData({
          info: tmpItem,
          timeObj: timeObj
        });
        this.initTimerObj(this.data.info);
      } else if (this.data.info.productInfo.groupInfo.status == 2) {
        // 隐藏页面
        this.setData({
          hidden: true
        });
      }
    },
    // 清除计时器
    clearTimer() {
      if (this.data.timerObj.timerId) {
        clearTimeout(this.data.timerObj.timerId);
        this.data.timerObj.timerId = null;
      }
    },
    // 点击去详情
    goDetail() {
      this.triggerEvent("detail", null, null);
    }
  }
})